package com.baihan.angus.app.game.busshow.ui.user;

import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.Observer;

import com.alibaba.android.arouter.launcher.ARouter;
import com.baihan.angus.app.game.basic.base.BaseFragment;
import com.baihan.angus.app.game.basic.utils.Tooast;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.databinding.FragmentUserBinding;
import com.baihan.angus.app.game.common.arouter_path.ArouterAppPath;
import com.baihan.angus.app.game.common.bean.response.Userinfo;
import com.baihan.angus.app.game.common.bean.response.VerifyStatusResponse;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * 我的
 */
public class UserFragment extends BaseFragment<FragmentUserBinding, UserVM> {

    private Userinfo userinfo;
    private VerifyStatusResponse.Data verifyStatusData;
    private String verifyId = "0";

    public UserFragment() {
    }

    public static UserFragment newInstance() {
        UserFragment fragment = new UserFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.userVM;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_user;
    }

    @Override
    protected void initView() {

        userinfo = UserConfig.getUserConfig().getUser();
        initUserData(userinfo);

//        LinearLayout.LayoutParams top = (LinearLayout.LayoutParams) mVDBinding.flTop.getLayoutParams();
//        top.topMargin = ImmersionBar.getStatusBarHeight(this);
//        mVDBinding.flTop.setLayoutParams(top);

        mVDBinding.tvEdit.setOnClickListener(view -> {
            ARouter.getInstance().build(ArouterAppPath.EDIT_USER_ACTIVITY).navigation();
        });

        mVDBinding.tvCopy.setOnClickListener(view -> {
            ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
            cm.setText(mVDBinding.tvId.getText());
            Tooast.normalInfo(getContext().getString(R.string.copySuccess));
        });

        //升级店家
        mVDBinding.llUpgradeStore.setOnClickListener(view -> {
            if (null == verifyStatusData || verifyStatusData.getType() == 4) {
                ARouter.getInstance().build(ArouterAppPath.STORE_FIRST_ACTIVITY).withString("verifyId", verifyId).navigation();
            } else {
                Tooast.normalInfo("您已进行其他认证");
            }
//            if (null == verifyStatusData || verifyStatusData.getVerify_status() == 3) {
//                ARouter.getInstance().build(ArouterAppPath.STORE_FIRST_ACTIVITY).withString("verifyId", verifyId).navigation();
//            } else {
//                if (verifyStatusData.getType() == 4) {
//                    ARouter.getInstance().build(ArouterAppPath.STORE_FIRST_ACTIVITY).withString("verifyId", verifyId).navigation();
//                } else {
//                    Tooast.normalInfo("您已进行其他认证");
//                }
//            }
        });

        //升级发行
        mVDBinding.llUpgradeIssuer.setOnClickListener(view -> {
            if (null == verifyStatusData || verifyStatusData.getType() == 3) {
                ARouter.getInstance().build(ArouterAppPath.ISSUER_FIRST_ACTIVITY).withString("verifyId", verifyId).navigation();
            } else {
                Tooast.normalInfo("您已进行其他认证");
            }
        });

        //升级编剧
        mVDBinding.llUpgradeWriter.setOnClickListener(view -> {
            if (null == verifyStatusData || verifyStatusData.getType() == 6) {
                ARouter.getInstance().build(ArouterAppPath.WRITER_FIRST_ACTIVITY).withString("verifyId", verifyId).navigation();
            } else {
                Tooast.normalInfo("您已进行其他认证");
            }
        });

        //升级DM
        mVDBinding.llUpgradeDm.setOnClickListener(view -> {
            if (null == verifyStatusData || verifyStatusData.getType() == 5) {
                ARouter.getInstance().build(ArouterAppPath.DM_FIRST_ACTIVITY).withString("verifyId", verifyId).navigation();
            } else {
                Tooast.normalInfo("您已进行其他认证");
            }
        });

        mVDBinding.rlAlbum.setOnClickListener(view -> {
            ARouter.getInstance().build(ArouterAppPath.ALBUM_ACTIVITY).navigation();
        });

        mVDBinding.rlHelp.setOnClickListener(view -> {
            ARouter.getInstance().build(ArouterAppPath.HELP_ACTIVITY).navigation();
        });

        mVDBinding.rlFeedback.setOnClickListener(view -> {
            ARouter.getInstance().build(ArouterAppPath.FEEDBACK_ACTIVITY).navigation();
        });

        mVDBinding.rlSetting.setOnClickListener(view -> {
            ARouter.getInstance().build(ArouterAppPath.SETTING_ACTIVITY).navigation();
        });

    }

    @Override
    protected void initData() {
        mViewModel.getVerifyStatus(userinfo.getUser_id());
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldStatus.observe(this, new Observer<VerifyStatusResponse.Data>() {
            @Override
            public void onChanged(VerifyStatusResponse.Data data) {
                verifyStatusData = data;
                if (null != data) {
                    verifyId = data.getUser_verify_id();
                }
            }
        });
    }

    private void initUserData(Userinfo userinfo) {
        mVDBinding.tvUserName.setText(userinfo.getNickname());
        mVDBinding.tvId.setText(userinfo.getUser_id());
        if (!TextUtils.isEmpty(userinfo.getBio())) {
            mVDBinding.tvIntro.setText(userinfo.getBio());
        } else {
            mVDBinding.tvIntro.setText(getString(R.string.introTip));
        }

        if (!TextUtils.isEmpty(userinfo.getBackground_image())) {
            Glide.with(this).asBitmap().load(userinfo.getBackground_image()).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    Drawable drawable = new BitmapDrawable(resource);
                    mVDBinding.flTop.setBackground(drawable);
                }
            });
        }

        int gender = userinfo.getGender();//0-女 1-男
        if (gender == 0) {
            mVDBinding.llSexAge.setBackgroundResource(R.mipmap.ic_women_bg);
            mVDBinding.ivSex.setImageResource(R.mipmap.ic_women);
            mVDBinding.tvAge.setTextColor(getActivity().getColor(R.color.textColor28));
        } else {
            mVDBinding.llSexAge.setBackgroundResource(R.mipmap.ic_man_bg);
            mVDBinding.ivSex.setImageResource(R.mipmap.ic_man);
            mVDBinding.tvAge.setTextColor(getActivity().getColor(R.color.textColor29));
        }

        mVDBinding.tvAge.setText(userinfo.getAge() + "");

        ImageLoader.loadImage(getActivity(), mVDBinding.ivUserAvatar, userinfo.getAvatar(), R.mipmap.ic_test_bg, 0);
    }

    @Override
    public void onResume() {
        super.onResume();
        userinfo = UserConfig.getUserConfig().getUser();
        initUserData(userinfo);
        mViewModel.getVerifyStatus(userinfo.getUser_id());
    }

}